package com.atf.atfconfigurator.views;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;

import com.atf.atfconfigurator.AtfconfiguratorUI;
import com.atf.atfconfigurator.composite.CollapseableBox;
import com.atf.atfconfigurator.utils.WasConstant;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.server.FontAwesome;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class JdbcProviderView extends VerticalLayout implements View{
	private PropertiesConfiguration wasConfigProperty;
	
	private List<String> jdbcParaList= Arrays.asList("Name", "Classpath", "Nativepath","Template","ImplementationClassName");
	@Override
	public void enter(ViewChangeEvent event) {
		// TODO Auto-generated method stub
		wasConfigProperty=((AtfconfiguratorUI) getUI()).getWasConfiguration();
		setSpacing(true);
        setMargin(true);
        Label title = new Label(wasConfigProperty.getString("appName"));
        title.addStyleName("h2");
        title.addStyleName("colored");
        
        
        addComponent(title);
        addComponent(new CollapseableBox("DB2 UDB for iseries DataSource Provider",getJDBCProvider("JDBCDB2iseries"), false));
        addComponent(new CollapseableBox("DB2 Universal JDBC Driver Provider",getJDBCProvider("DB2Universal"), false));
        addComponent(new CollapseableBox("Oracle JDBC Driver Provider",getJDBCProvider("JDBCOracle"), false));
        addComponent(new CollapseableBox("Microsoft SQL Server JDBC Driver Provider",getJDBCProvider("JDBCMSSQL"), false));
        addComponent(new CollapseableBox("TerraData JDBC Provider",getJDBCProvider("JDBCTerraData"), false));
	}
	
	
	
	public FormLayout getJDBCProvider(final String type){
		
		final FormLayout form = new FormLayout();
		//application Name
		form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        CheckBox check = new CheckBox("Create");
        if( wasConfigProperty.getString("create"+type, "").equalsIgnoreCase("YES")){
        	check.setValue(true);
        }
        check.addValueChangeListener(new Property.ValueChangeListener(){

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				if( (Boolean) event.getProperty().getValue()){
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("create"+type, "YES");
				}else{
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("create"+type, "NO");
				}
			}        	
        });
        form.addComponent(check);
        if( wasConfigProperty.containsKey("numberof"+type)){
        	//Department Name
            final TextField nJDBC = new TextField("Number of Provider");
            int value=wasConfigProperty.getInt("numberof"+type);
            nJDBC.setValue(String.valueOf(value));
            nJDBC.setWidth("50%");
            //nJDBC.setIcon(FontAwesome.PLUS);
            nJDBC.addValueChangeListener(new Property.ValueChangeListener(){

    			/**
    			 * 
    			 */
    			private static final long serialVersionUID = 1L;

    			@Override
    			public void valueChange(ValueChangeEvent event) {
    				// TODO Auto-generated method stub
    				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("numberof"+type,  (String) event.getProperty().getValue());
    				((AtfconfiguratorUI) getUI()).navigator.navigateTo("JdbcProvider");
    			}        	
            });
            form.addComponent(nJDBC);
            for(int i=1; i<value+1; i++){
            	form.addComponent(new CollapseableBox(String.valueOf(i),getJDBCProviderEntries(type, i), true));
            }
            
            
        }else{
        	form.addComponent(getJDBCProviderEntries(type, 0));
        }
        
        return form;
	}
	
	public FormLayout getJDBCProviderEntries(final String type, final int pos){
		final FormLayout form = new FormLayout();
		//application Name
		form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        
        if(pos == 0){
        	for(String par:jdbcParaList){
        		String value;
        		if( wasConfigProperty.containsKey(type+par)){
        			value=wasConfigProperty.getString(type+par);
        		}else{
        			value=WasConstant.getPropertyValue(WasConstant.WAS_XML_JDBCPROVIDER_NAME+"."+type+"."+par);
        			((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty(type+par,value);
        		}
        		
        		final TextField nJDBC = new TextField(par);
                nJDBC.setValue(value);
                nJDBC.setWidth("100%");
                nJDBC.addValueChangeListener(new Property.ValueChangeListener(){

					@Override
					public void valueChange(ValueChangeEvent event) {
						// TODO Auto-generated method stub
						String value = (String) event.getProperty().getValue();
						((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty(type+nJDBC.getCaption(), value);
					}
                	
                });
                nJDBC.setImmediate(true);
                form.addComponent(nJDBC);
        	}
        }else{
        	for(String par:jdbcParaList){
        		String value;
        		if( wasConfigProperty.containsKey(type+pos+par)){
        			value=wasConfigProperty.getString(type+pos+par);
        		}else{
        			value=WasConstant.getPropertyValue(WasConstant.WAS_XML_JDBCPROVIDER_NAME+"."+type+"."+par);
        			((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty(type+pos+par,value);
        		}
        		final TextField nJDBC = new TextField(par);
                nJDBC.setValue(value);
                nJDBC.setWidth("100%");
                nJDBC.addValueChangeListener(new Property.ValueChangeListener(){

					@Override
					public void valueChange(ValueChangeEvent event) {
						// TODO Auto-generated method stub
						String value = (String) event.getProperty().getValue();
						((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty(type+pos+nJDBC.getCaption(), value);
					}
                	
                });
                nJDBC.setImmediate(true);
                form.addComponent(nJDBC);
        	}
        }
        return form;
	}

}
